package cat.sun.dialogs;

import cat.sun.R;
import cat.sun.activities.Main;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class SettingsDialog extends DialogFragment
{
	public static SettingsDialog newInstance()
	{
		SettingsDialog dialog = new SettingsDialog();		
		return dialog;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{		
		super.onCreate(savedInstanceState);
		setStyle(DialogFragment.STYLE_NO_FRAME, 0);	
		//setCancelable(false);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
	{
		View view = inflater.inflate(R.layout.dialog_settings, container,false);		
		final Context context = getActivity();		
		
		TextView title = (TextView) view.findViewById(R.id.dialog_options_title);
		title.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"));
		
		TextView rateT = (TextView) view.findViewById(R.id.dialog_options_rate_text);
		rateT.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
		
		TextView vibroT = (TextView) view.findViewById(R.id.dialog_options_vibro_text);
		vibroT.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
		
		TextView aboutT = (TextView) view.findViewById(R.id.dialog_options_about_text);
		aboutT.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
				
		TextView vers = (TextView) view.findViewById(R.id.dialog_settings_version);
		PackageInfo pInfo;
		try 
		{
			pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			vers.setText(getResources().getString(R.string.AppVer)+" "+pInfo.versionName);
		} catch (NameNotFoundException e) 
		{
			e.printStackTrace();
		}
		vers.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
				
		final ImageView vibro = (ImageView) view.findViewById(R.id.dialog_settings_vibration);
		final ImageView rate = (ImageView) view.findViewById(R.id.dialog_settings_rate_us);
		final ImageView about = (ImageView) view.findViewById(R.id.dialog_settings_about);
		
		boolean pressed = context.getSharedPreferences(Main.OPTIONS, 0).getBoolean(Main.VIBRATION, true);
		if (pressed) 
			vibro.setImageResource(R.drawable.vibration_button_on);
		else
			vibro.setImageResource(R.drawable.vibration_button_off);
		
		rate.setOnClickListener(new View.OnClickListener() 
		{			
			@Override
			public void onClick(View v) 
			{
				context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + "cat.sun")));
			}
		});
		
		about.setOnClickListener(new View.OnClickListener() 
		{		
			@Override
			public void onClick(View v) 
			{
				LayoutInflater inflater = getActivity().getLayoutInflater();
				View layout = inflater.inflate(R.layout.toast_about, (ViewGroup) getActivity().findViewById(R.id.toast_about_id));
				Toast toast = new Toast(getActivity().getApplicationContext());
				toast.setDuration(Toast.LENGTH_LONG);
				toast.setView(layout);
				toast.show();
			}
		});
		
		vibro.setOnClickListener(new View.OnClickListener() 
		{			
			@Override
			public void onClick(View v) 
			{
				SharedPreferences.Editor editor = context.getSharedPreferences(Main.OPTIONS, 0).edit();
 				editor.putBoolean(Main.VIBRATION, !context.getSharedPreferences(Main.OPTIONS, 0).getBoolean(Main.VIBRATION, true)); 				
 				editor.commit();
 				boolean pressed = context.getSharedPreferences(Main.OPTIONS, 0).getBoolean(Main.VIBRATION, true);
 				if (pressed) 
 					vibro.setImageResource(R.drawable.vibration_button_on);
 				else
 					vibro.setImageResource(R.drawable.vibration_button_off);
			}
		});
				
		
		final ImageView close = (ImageView) view.findViewById(R.id.dialog_settings_close);
		
		close.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				dismiss();
			}			
		});
		return view;
	}
	
	
}
